package com.erp4j.dynamicfield.controller;

import com.erp4j.dynamicfield.entity.FieldDefinition;
import com.erp4j.dynamicfield.service.FieldDefinitionService;
import com.erp4j.dynamicfield.vo.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/field-configs")
public class FieldConfigController {

    @Resource
    private FieldDefinitionService fieldDefinitionService;

    /**
     * 获取所有字段配置
     */
    @GetMapping
    public Result<List<FieldDefinition>> getAllFieldConfigs() {
        return Result.success(fieldDefinitionService.list());
    }

    /**
     * 根据模块ID获取字段配置
     */
    @GetMapping("/module/{moduleId}")
    public Result<List<FieldDefinition>> getFieldConfigsByModuleId(@PathVariable Long moduleId) {
        return Result.success(fieldDefinitionService.getFieldsByModuleId(moduleId));
    }

    /**
     * 根据ID获取字段配置
     */
    @GetMapping("/{id}")
    public Result<FieldDefinition> getFieldConfig(@PathVariable Long id) {
        return Result.success(fieldDefinitionService.getById(id));
    }

    /**
     * 保存或更新字段配置
     */
    @PostMapping
    public Result<FieldDefinition> saveFieldConfig(@RequestBody FieldDefinition config) {
        fieldDefinitionService.saveOrUpdate(config);
        return Result.success(config);
    }

    /**
     * 删除字段配置
     */
    @DeleteMapping("/{id}")
    public Result<?> deleteFieldConfig(@PathVariable Long id) {
        fieldDefinitionService.removeById(id);
        return Result.success();
    }
}
